package com.thoughtworks.biblioteca;

import com.thoughtworks.biblioteca.controller.BookController;
import com.thoughtworks.biblioteca.controller.MenuController;
import com.thoughtworks.biblioteca.command.Invoker;
import com.thoughtworks.biblioteca.command.ICommand;
import com.thoughtworks.biblioteca.util.ConsoleUtil;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class Biblioteca {


    private BookController bookController;
    private MenuController menuController;

    private Invoker invoker;

    private ICommand listBookCatalog;
    private ICommand checkOutBook;
    private ICommand showMovieDetail;
    private ICommand checkLibraryNumber;
    private ConsoleUtil consoleUtil;


    private void initCommand() {
        invoker.addComm("1", listBookCatalog);
        invoker.addComm("2", checkOutBook);
        invoker.addComm("3", checkLibraryNumber);
        invoker.addComm("4", showMovieDetail);
    }

    public static void main(String[] args) {

        Resource resource = new ClassPathResource("spring-config.xml");
        BeanFactory factory = new XmlBeanFactory(resource);

        Biblioteca program = (Biblioteca) factory.getBean("mainProgram");
        program.initCommand();

        while (true) {
            program.initMenu();

            String choosedMenuID = "" + program.getChooseMenuID();
            program.invoker.request(choosedMenuID);

        }
    }

    void initMenu() {
        menuController.printMenu();
    }


    private int getChooseMenuID() {
        System.out.println("Your Selection: ");
        return consoleUtil.getNumberFromConsole();
    }

    public BookController getBookController() {
        return bookController;
    }

    public void setBookController(BookController bookController) {
        this.bookController = bookController;
    }

    public MenuController getMenuController() {
        return menuController;
    }

    public void setMenuController(MenuController menuController) {
        this.menuController = menuController;
    }

    public Invoker getInvoker() {
        return invoker;
    }

    public void setInvoker(Invoker invoker) {
        this.invoker = invoker;
    }

    public ICommand getListBookCatalog() {
        return listBookCatalog;
    }

    public void setListBookCatalog(ICommand listBookCatalog) {
        this.listBookCatalog = listBookCatalog;
    }

    public ICommand getCheckOutBook() {
        return checkOutBook;
    }

    public void setCheckOutBook(ICommand checkOutBook) {
        this.checkOutBook = checkOutBook;
    }

    public ICommand getShowMovieDetail() {
        return showMovieDetail;
    }

    public void setShowMovieDetail(ICommand showMovieDetail) {
        this.showMovieDetail = showMovieDetail;
    }

    public ICommand getCheckLibraryNumber() {
        return checkLibraryNumber;
    }

    public void setCheckLibraryNumber(ICommand checkLibraryNumber) {
        this.checkLibraryNumber = checkLibraryNumber;
    }

    public ConsoleUtil getConsoleUtil() {
        return consoleUtil;
    }

    public void setConsoleUtil(ConsoleUtil consoleUtil) {
        this.consoleUtil = consoleUtil;
    }
}
